package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.common.ArchiverConstant;
import com.iwhalecloud.uncc.common.ArchiverThreadLocal;
import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.domain.ArchiveParam;
import com.iwhalecloud.uncc.model.InsServiceActiveLog;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/26 23:31
 * @description
 */
@Repository("insServiceActiveLogArchiverDao")
public class InsServiceActiveLogArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {
        return null;
    }

    @Override
    public List queryList(Object o) {
        StringBuilder sql = new StringBuilder("SELECT * FROM INS_SERVICE_ACTIVE_LOG WHERE SERVICE_ID = ? AND HANDLE_TIME BETWEEN ? AND ? AND ARCHIVER_FLAG = " + ArchiverConstant.ONLINE);
        ArchiveParam aP = ArchiverThreadLocal.getArchiveParam();
        List<InsServiceActiveLog> list = jdbcTemplate.query(sql.toString(), new Object[]{o,aP.getStartDate(),aP.getEndDate()} , new BeanPropertyRowMapper<>(InsServiceActiveLog.class));
        if (!list.isEmpty()) {
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `ID`, `SERVICE_ID`, `HANDLE`, `HANDLE_TIME`, `HANDLE_OP`, `PARTITION_CODE`");
        sql.append(" FROM INS_SERVICE_ACTIVE_LOG WHERE SERVICE_ID in (:i)");
        ArchiveParam aP = ArchiverThreadLocal.getArchiveParam();
        int limit = (list.size())*(aP.getEachInsLines());
        sql.append(" AND HANDLE_TIME BETWEEN \"" + aP.getStartDate() + "\" AND \"" + aP.getEndDate()+"\"");
        sql.append(" AND ARCHIVER_FLAG = " + ArchiverConstant.ONLINE);
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        sql.append(" LIMIT " + limit);
        List<InsServiceActiveLog> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                InsServiceActiveLog insServiceActiveLog = new InsServiceActiveLog();
                insServiceActiveLog.setId(resultSet.getLong("ID"));
                insServiceActiveLog.setServiceId(resultSet.getLong("SERVICE_ID"));
                insServiceActiveLog.setHandle(resultSet.getInt("HANDLE"));
                insServiceActiveLog.setHandleTime(resultSet.getTimestamp("HANDLE_TIME"));
                insServiceActiveLog.setHandleOp(resultSet.getString("HANDLE_OP"));
                insServiceActiveLog.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                return insServiceActiveLog;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `ins_service_active_log`");
        sql.append(" (`ID`, `SERVICE_ID`, `HANDLE`, `HANDLE_TIME`, `HANDLE_OP`, `PARTITION_CODE`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:id,:serviceId,:handle,:handleTime,:handleOp,:partitionCode,:archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {
        StringBuilder sql = new StringBuilder("UPDATE INS_SERVICE_ACTIVE_LOG SET ARCHIVER_FLAG = " + flag + " WHERE ID = :id");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM INS_SERVICE_ACTIVE_LOG WHERE ID = :id");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
